﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using CNotebook;

namespace NotebookS2
{
    public partial class CRYSTAL_AR_AGING : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public CRYSTAL_AR_AGING()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public DateTime AsofDate { get; set; }
        public bool IsByDeliveryDate { get; set; }
        ReportDocument rptDoc = new ReportDocument();
        SqlConnection oConnection = new SqlConnection();
        SqlCommand oCommand = new SqlCommand();
        SqlDataAdapter oAdapter = new SqlDataAdapter();
        DataSet ds = new DataSet();


        public string GetLayout()
        {
            ReportConfigManager oManager = new ReportConfigManager();
            const int _DOCUMENT_TYPE = 44;
            oManager.ConnectionString = this.ConnectionString;

            oManager.Open();

            string _FILE_NAME = oManager.GetReportFileName(_DOCUMENT_TYPE);

            oManager.Close();

            return _FILE_NAME;
        }

        public void ViewSalesOrderLogs()
        {
            rptDoc = new ReportDocument();
            oConnection = new SqlConnection();
            oCommand = new SqlCommand();
            oAdapter = new SqlDataAdapter();
            ds = new DataSet();

            try
            {
                string sReportFile = "";

                sReportFile = GetLayout();

                rptDoc.Load(sReportFile);





                oConnection.ConnectionString = this.ConnectionString;
                oConnection.Open();

                oCommand.Connection = oConnection;
                oCommand.CommandType = CommandType.StoredProcedure;
                oCommand.CommandText = "SP_AGING_REPORT";

                int iDeliveryIndex = 0;

                if (this.IsByDeliveryDate == true)
                {
                    iDeliveryIndex = 0;
                }
                else
                {
                    iDeliveryIndex = 1;
                }
                oCommand.Parameters.Add(new SqlParameter("@ASOF", this.AsofDate));
                oCommand.Parameters.Add(new SqlParameter("@ByDeliveryDate", iDeliveryIndex));

                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(ds);

                oConnection.Close();

                rptDoc.SetDataSource(ds.Tables[0]);

                ParameterField oFrom = new ParameterField();
                ParameterField oTo = new ParameterField();



                ParameterFields oFields = new ParameterFields();
                rptDoc.SetParameterValue(0, this.AsofDate);
                rptDoc.SetParameterValue(1, iDeliveryIndex);

                CRViewer.ReportSource = rptDoc;
                CRViewer.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            //finally
            //{
            //    rptDoc.Dispose();
            //    oConnection.Dispose();
            //    oCommand.Dispose();
            //    oAdapter.Dispose();
            //    ds.Dispose();

            //}
        }

        private void CRYSTAL_SALESORDER_Load(object sender, EventArgs e)
        {
            ViewSalesOrderLogs();
        }

        private void CRYSTAL_AR_AGING_FormClosing(object sender, FormClosingEventArgs e)
        {
            rptDoc.Dispose();
            oConnection.Dispose();
            oCommand.Dispose();
            oAdapter.Dispose();
            ds.Dispose();
        }
    }
}
